// 引入async
const async = require('async');
// 引入model实例
const carouselModel = require('../util/models/carousel');
const videoModel = require('../util/models/video');
const usersModel = require('../util/models/users');
const commentModel = require('../util/models/comment');

const Common = require('../util/common')
const db = require('../util/db');
const moment = require('moment')
const Constant = require('../util/constant');

// 配置导出对象
let exportObj = {
    getCarousel,
    getVideo,
    getDetail,
    getUsers,
    getCommentByvideo,
    getCommentByuser,
    getVideoByuser,
};

module.exports = exportObj;

function getCarousel(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        query: cb => {
            carouselModel
                .findAll({
                    limit: 4,
                    order: [
                        ['id', 'DESC']
                    ],
                })
                .then(function(result) {
                    let list = [];
                    result.forEach((v, i) => {
                        let obj = {
                            id: v.id,
                            img: v.img,
                            title: v.title,
                            backgroundColor: v.backgroundColor
                        };
                        list.push(obj);
                    });
                    resObj.data = {
                        list
                    }
                    cb(null);
                })
                .catch(function(err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });

        }
    };

    Common.autoFn(tasks, res, resObj)
}

tasks = {
    query: cb => {
        videoModel
            .findAll({
                offset: parseInt(req.query.offset) || 0,
                limit: parseInt(req.query.limit) || 4,
                order: [
                    ['id', 'ASC']
                ]
            })
            .then(function(result) {
                video = result
                console.log(1);
                cb(null);
            })
    },
    change: ['query', (result, cb) => {
        let Commentcount
        let nickname
        video.forEach(async(v, i) => {
            res = await commentModel.findAndCountAll({ where: { video_id: v.id } })
            Commentcount = res.count
            res = await usersModel.findOne({ where: { id: v.up } })
            nickname = res.nickname
            obj = {
                id: v.id,
                title: v.title,
                up: v.up,
                img: v.img,
                views: v.views,
                likes: v.likes,
                stars: v.stars,
                shares: v.shares,
                coins: v.coins,
                link: v.link,
                introduction: v.introduction,
                viewsChange: Common.numChange(v.views),
                likesChange: Common.numChange(v.likes),
                starsChange: Common.numChange(v.stars),
                sharesChange: Common.numChange(v.shares),
                coinsChange: Common.numChange(v.coins),
                commentCount: Commentcount,
                nickname: nickname
            };
            list.push(obj);
            console.log(2);
        })
        console.log(3);
        cb(null)
    }],
    end: ['change', (result, cb) => {
        console.log(4);
        resObj.data = list
        cb(null)
    }]
};


function getVideo(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let video
    let list = [];
    let tasks = {
        query: cb => {
            videoModel
                .findAll({
                    offset: parseInt(req.query.offset) || 0,
                    limit: parseInt(req.query.limit) || 4,
                    // order: db.random()
                })
                .then(function(result) {
                    video = result
                    cb(null);
                })
        },
        change: ['query', async(result, cb) => {
            let Commentcount
            let nickname
            for (let i = 0; i < video.length; i++) {
                const v = video[i];
                res = await commentModel.findAndCountAll({ where: { video_id: v.id } })
                Commentcount = res.count
                res = await usersModel.findOne({ where: { id: v.up } })
                nickname = res.nickname
                obj = {
                    id: v.id,
                    title: v.title,
                    up: v.up,
                    img: v.img,
                    views: v.views,
                    likes: v.likes,
                    stars: v.stars,
                    shares: v.shares,
                    coins: v.coins,
                    link: v.link,
                    introduction: v.introduction,
                    viewsChange: Common.numChange(v.views),
                    likesChange: Common.numChange(v.likes),
                    starsChange: Common.numChange(v.stars),
                    sharesChange: Common.numChange(v.shares),
                    coinsChange: Common.numChange(v.coins),
                    commentCount: Commentcount,
                    nickname: nickname
                };
                list.push(obj);
            }
            resObj.data = list
        }],
    };
    Common.autoFn(tasks, res, resObj)
}


function getDetail(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        query: cb => {
            let Commentcount
            videoModel
                .findOne({ where: { id: req.params.id } })
                .then(async function(result) {
                    res = await commentModel.findAndCountAll({ where: { video_id: result.id } })
                    Commentcount = res.count
                    res = await usersModel.findOne({ where: { id: result.up } })
                    obj = {
                        id: result.id,
                        title: result.title,
                        up: result.up,
                        img: result.img,
                        views: result.views,
                        likes: result.likes,
                        stars: result.stars,
                        shares: result.shares,
                        coins: result.coins,
                        link: result.link,
                        introduction: result.introduction,
                        viewsChange: Common.numChange(result.views),
                        likesChange: Common.numChange(result.likes),
                        starsChange: Common.numChange(result.stars),
                        sharesChange: Common.numChange(result.shares),
                        coinsChange: Common.numChange(result.coins),
                        commentCount: Commentcount,
                        nickname: res.nickname,
                        avatar: res.avatar,
                        followsChange: Common.numChange(res.follows),
                        fansChange: Common.numChange(res.fans),
                        upinrtoduction: res.introduction,
                        createdAt: moment(res.createdAt).format('YYYY-MM-DD:HH:MM:SS')
                    };
                    resObj.data = obj
                    cb(null);
                })
                .catch(function(err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });
        }
    };
    Common.autoFn(tasks, res, resObj)
}

function getUsers(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        query: cb => {
            usersModel
                .findOne({ where: { id: req.params.id } })
                .then(function(result) {
                    result.password = ''
                    resObj.data = result

                    cb(null);
                })
                .catch(function(err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });
        }
    };
    Common.autoFn(tasks, res, resObj)
}

function getCommentByvideo(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        query: cb => {
            commentModel
                .findAndCountAll({
                    where: { video_id: req.params.id },
                    order: [
                        ['created_at', 'DESC']
                    ]
                })
                .then(function(result) {
                    resObj.data = {
                        result
                    }
                    cb(null);
                })
                .catch(function(err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });

        }
    };
    Common.autoFn(tasks, res, resObj)
}

function getCommentByuser(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        query: cb => {
            commentModel
                .findAndCountAll({
                    where: { user_id: req.params.id },
                    order: [
                        ['created_at']
                    ]
                })
                .then(function(result) {
                    resObj.data = {
                        result
                    }
                    cb(null);
                })
                .catch(function(err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });

        }
    };
    Common.autoFn(tasks, res, resObj)
}

function getVideoByuser(req, res) {
    const resObj = Common.clone(Constant.DEFAULT_SUCCESS);
    let tasks = {
        query: cb => {
            videoModel
                .findAndCountAll({
                    where: { up: req.params.id },
                    order: [
                        ['created_at']
                    ]
                })
                .then(function(result) {
                    resObj.data = {
                        result
                    }
                    cb(null);
                })
                .catch(function(err) {
                    console.log(err);
                    cb(Constant.DEFAULT_ERROR);
                });

        }
    };
    Common.autoFn(tasks, res, resObj)
}